#!/usr/bin/perl
#    TigerLily:  A client for the lily CMC, written in Perl.
#    Copyright (C) 1999  The TigerLily Team, <tigerlily@einstein.org>
#                                http://www.hitchhiker.org/tigerlily/
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License version 2, as published
#  by the Free Software Foundation; see the included file COPYING.
# $Header: /home/mjr/tmp/tlilycvs/lily/tigerlily2/tools/Attic/prerelease,v 1.2 1999/04/11 22:35:49 albert Exp $

use strict;
use Config;
use TLily::Version;

use vars qw($nonono $debug $release_state %version);

$nonono = 0;
$debug  = 0;
$release_state = $debug ? 'test' : 'release';

%version = version_from_string($ARGV[0]);
my $CVSTag = cvstag_from_version($release_state, %version);

if(! -d "CVS") {
    print STDERR "You do not appear to be a developer.  You cannot ",
	"use this function.\n";
    exit(1);
}

#%version = next_version(%version);
#ask_version();
make_version_file(%version);

my $cmd = "cvs commit -m 'Automatic precommit of version file' TLily/Version.pm";
if($nonono) {
    print "Would have executed: $cmd\n";
}
else {
    print "Committing TLily/Version.pm with:\n$cmd\n";
    system($cmd) and die "Command returned error!\n";
}

make_changelog(%version);

$cmd = "cvs commit -m 'Automatic commit of ChangeLog file' ChangeLog";
if($nonono) {
    print "Would have executed: $cmd\n";
}
else {
    print "Committing ChangeLog with:\n$cmd\n";
    system($cmd) and die "Command returned error!\n";
}

$cmd = "cvs tag -F $CVSTag";
if($nonono) {
    print "Would have executed: $cmd\n";
}
else {
    print "Tagging release ", string_from_version(%version), " with:\n$cmd\n";
    system("$cmd") and die "Command returned error!\n";
}

exit(0);

sub Usage {
    die "\nUsage: $0\n";
}

sub ask {
    $|=1;
    my($q,$d) = @_;
    if(length($q. " [". $d. "]: ") > 80) {
	print $q,"\n[", $d, "]: ";
    }
    else {
	print $q, " [", $d, "]: ";
    }
    my $l = <STDIN>;
    chomp $l;
    $l eq '' ? $d : $l;
}

sub ask_version {
    my $inp;

    while(1) {
	$inp = ask("Release Version?", string_from_version(%version));
	check_version($inp) or warn "Invalid Version.  Please try again.\n"
	    and next;
        last;
    }
    
    %version = version_from_string($inp);
    $CVSTag = cvstag_from_version($release_state, %version);
    print "VERSION=", string_from_version(%version), "\n",
          "CVSTag =", $CVSTag, "\n" if $debug;
}

sub make_version_file {
    my(%v) = @_;

    if($nonono) {
	print "Would have made TLily/Version.pm with version number ",
	    string_from_version(%v), "\n";
	return;
    }

    print "Writing TLily/Version.pm, version ", string_from_version(%v), "\n";

    -f "TLily/Version.pm" and unlink "TLily/Version.pm"
	or die "Cannot unlink TLily/Version.pm: $!\n";

    local(*VERFILE);
    my $ver = string_from_version(%v);
    open(VERFILE, ">TLily/Version.pm")
	or die "Cannot open TLily/Version.pm for writing: $!\n";

    print VERFILE qq(#    TigerLily:  A client for the lily CMC, written in Perl.
#    Copyright (C) 1999  The TigerLily Team, <tigerlily\@einstein.org>
#                                http://www.hitchhiker.org/tigerlily/
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License version 2, as published
#  by the Free Software Foundation; see the included file COPYING.
#
# Version.pm - Automatically generated by tools/prerelease
package TLily::Version;
\$VERSION = '$ver';
1;\n);
    close VERFILE;
    chmod(0444, "TLily/Version.pm")or die "Cannot chmod TLily/Version.pm: $!\n";
}

sub make_changelog {
    my(%v) = @_;

    if($nonono) {
	print "Would have made ChangeLog with version number ",
	    string_from_version(%v), "\n";
	return;
    }

    print "Writing ChangeLog, version ", string_from_version(%v), "\n";

    local(*CL);
    open(CL, "ChangeLog") or die "Cannot open ChangeLog for reading: $!\n";
    my @lines = <CL>;
    close CL;

    unlink('ChangeLog') or die "Cannot unlink ChangeLog: $!\n";

    $lines[0] = "Version ".string_from_version(%v)."\n";;

    open(CL, ">ChangeLog") or die "Cannot open ChangeLog for writing: $!\n";
    print CL @lines;
    close CL;

    chmod(0644, "ChangeLog") or die "Cannot chmod ChangeLog: $!\n";
}

sub cvstag_from_version {
    my($s, %v) = @_;

    my $tag = "$s-$v{major}-$v{minor}";
    if($v{sub})  { $tag .= "-$v{sub}"; }
    if($v{pre})  { $tag .= "-pre";     }
    if($v{post}) { $tag .= "-post";    }
    $tag;
}

sub version_from_string {
    my($v) = @_;
    my(%v);

    $v =~ /(\d+)\.(\d+)\.?(\d*)/;

    $v{major} = $1;
    $v{minor} = $2;
    $v{sub}   = $3;

    $v =~ /\d+\.\d+(?:\.\d+)?-(pre|post)/;

    if($1) { $v{$1} = $1; }

    %v;
}

sub next_version {
    my(%v) = @_;
    delete $v{pre};
    delete $v{post};
    $v{sub}++;
    %v;
}

sub string_from_version {
    my(%v) = @_;

    $v{major} . '.' . $v{minor} .
	($v{sub}  ? '.' . $v{sub} : "") .
	($v{pre}  ? '-pre'  : "") .
	($v{post} ? '-post' : "");
}

sub check_version {
    my($s) = @_;
    my(%v) = version_from_string($s);
    if($v{major} =~ /^\d+$/ && $v{minor} =~ /^\d+$/) {
       return 1;
    }
}
